package com.okyai.cms.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.okyai.common.annotation.Log;
import com.okyai.common.enums.BusinessType;
import com.okyai.cms.domain.CmsCategory;
import com.okyai.cms.service.ICmsCategoryService;
import com.okyai.common.core.controller.BaseController;
import com.okyai.common.core.domain.AjaxResult;
import com.okyai.common.utils.poi.ExcelUtil;
import com.okyai.common.utils.StringUtils;
import com.okyai.common.core.domain.Ztree;

/**
 * 分类Controller
 * 
 * @author okyai
 * @date 2020-12-07
 */
@Controller
@RequestMapping("/cms/category")
public class CmsCategoryController extends BaseController
{
    private String prefix = "cms/category";

    @Autowired
    private ICmsCategoryService cmsCategoryService;

    @RequiresPermissions("cms:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询分类树列表
     */
    @RequiresPermissions("cms:category:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CmsCategory> list(CmsCategory cmsCategory)
    {
        List<CmsCategory> list = cmsCategoryService.selectCmsCategoryList(cmsCategory);
        return list;
    }

    /**
     * 导出分类列表
     */
    @RequiresPermissions("cms:category:export")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsCategory cmsCategory)
    {
        List<CmsCategory> list = cmsCategoryService.selectCmsCategoryList(cmsCategory);
        ExcelUtil<CmsCategory> util = new ExcelUtil<CmsCategory>(CmsCategory.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 新增分类
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsCategory", cmsCategoryService.selectCmsCategoryById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存分类
     */
    @RequiresPermissions("cms:category:add")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsCategory cmsCategory)
    {
        return toAjax(cmsCategoryService.insertCmsCategory(cmsCategory));
    }

    /**
     * 修改分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsCategory cmsCategory = cmsCategoryService.selectCmsCategoryById(id);
        mmap.put("cmsCategory", cmsCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存分类
     */
    @RequiresPermissions("cms:category:edit")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsCategory cmsCategory)
    {
        return toAjax(cmsCategoryService.updateCmsCategory(cmsCategory));
    }

    /**
     * 删除
     */
    @RequiresPermissions("cms:category:remove")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(cmsCategoryService.deleteCmsCategoryById(id));
    }

    /**
     * 选择分类树
     */
    @GetMapping(value = { "/selectCategoryTree/{id}", "/selectCategoryTree/" })
    public String selectCategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsCategory", cmsCategoryService.selectCmsCategoryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = cmsCategoryService.selectCmsCategoryTree();
        return ztrees;
    }
}
